from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)
students =[{'id': 1, 'name': '张三', 'age': 13, 'class':'软件1班',
            'email': '123@qq.com', 'address': '深圳市龙岗区'},
            {'id': 2, 'name': '李美丽', 'age': 12, 'class':'软件1班',
             'email': '234@qq.com', 'address': '深圳市宝安区'},
           {'id': 3, 'name': '刘德华', 'age': 11, 'class':'软件1班',
            'email': '555@qq.com', 'address': '深圳市福田区'},
           {'id': 4, 'name': '梁朝伟', 'age': 12, 'class':'软件1班',
            'email': '333@qq.com', 'address': '深圳市罗湖区'}
            ]


@app.route('/student')
def s_list():
    stu_list = []
    search_name = request.args.get('search')
    if search_name:
        for i in students:
            if search_name in i.get('name'):
                stu_list.append(i)
        return render_template('list_student.html', students=stu_list)
    return render_template('list_student.html', students=students)


@app.route('/add', methods=['get', 'post'])
def add():
    error = None
    error_list = []
    if request.method == 'POST':
        id = request.values.get('id')
        name = request.values.get('name')
        age = request.values.get('age')
        s_class = request.values.get('class')
        email = request.values.get('email')
        address = request.values.get('address')
        try:
            sid = int(id)
            if sid in [i.get('id') for i in students]:
                error_list.append('您输入的学号重复了')
        except:
            error_list.append('您输入的id格式不正确')
        try:
            int_age = int(age)
            if int_age < 0 or int_age > 200:
                error_list.append('您输入年龄超过限制')
        except:
            error_list.append('您输入的年龄格式不正确')
            if not error_list:
                dic = {'id': sid, 'name': name, 'age': int_age, "class": s_class,
                       'email': email, 'address': address}
                students.append(dic)
                return redirect(url_for('s_list'))
    return render_template('add.html', error=",".join(error_list))


@app.route('/modify/<int:sid>', methods=['get', 'post'])
def modify(sid):
    student = next(iter([stu for stu in students if str(stu.get('id')) == str(sid)]), {})
    if not student:
        return '用户不存在', 404
    error = None
    if request.method == 'POST':
        name = request.form.get('name')
        age = request.form.get('age')
        s_class = request.form.get('class')
        email = request.form.get('email')
        address = request.form.get('address')
        try:
            int_age = int(age)
            if int_age < 0 or int_age > 200:
                error = '您输入的年龄超过限制'
        except:
            error = '您输入的年龄格式不正确'
        if not error:
            student['name'] = name
            student['age'] = int_age
            student['class'] = s_class
            student['email'] = email
            student['address'] = address
            return redirect(url_for('s_list'))
    return render_template('modify_student.html', student=student, error=error)


@app.route('/delete/<int:sid>')
def delete(sid):
    global students
    students = [stu for stu in students if str(sid) != str(stu.get('id'))]
    return render_template('list_student.html', students=students)


if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5020, debug=True)